package gw.com.android.presenter.http;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import gw.com.android.app.AppMain;
import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import gw.com.android.utils.CacheUtils;
import okhttp3.Request;
import okhttp3.Response;
import www.com.library.app.Logger;
import www.com.library.util.DeviceUtil;
import www.com.library.util.JsonUtil;

/**
 * @author jett
 * @since 2018-12-17.
 */
public class ConfigFileRequest {

    /**
     * 获取本地版本号
     */
    public static void getVersion() {
        String url = ConfigUtil.instance().getUrlPath(ConfigType.CONFIG_VERSION_TAG);
        url = url.replace(ConfigType.REPLACE_APP_VERSION_CODE_TAG, DeviceUtil.instance().appVersionCode(AppMain.getApp()) + "");
        Logger.i("ConfigFileRequest", "start. url = " + url);
        new Https(url).get(new Https.ResponseCallback<String>() {
            @Override
            public void onSuccess(Request request, Response response, String result, int code) {
                String configVersion = GTConfig.instance().getConfigVersion();
                Logger.i("ConfigFileRequest", "onSuccess. result = " + result + "\nlocal configiration version = " + configVersion);
                if (null != result && JsonUtil.isNumeric(result) && !configVersion.equals(result)) {
                    getContent(result);
                }
            }

            @Override
            public void onFailure(Request request, IOException e) {
                Logger.e("ConfigFileRequest", e);
            }
        });
    }

    /**
     * 获取服务器上的json文件
     */
    private static void getContent(final String versionCode) {
        String url = ConfigUtil.instance().getUrlPath(ConfigType.CONFIG_FUN_TAG);
        url = url.replace(ConfigType.REPLACE_APP_VERSION_CODE_TAG, DeviceUtil.instance().appVersionCode(AppMain.getApp()) + "");
        url = url.replace(ConfigType.REPLACE_VERSION_CODE_TAG, versionCode);
        Logger.i("ConfigFileRequest", "getContent. url = " + url);
        new Https(url).get(new Https.ResponseCallback<String>() {
            @Override
            public void onSuccess(Request request, Response response, String result, int code) {
                Logger.i("ConfigFileRequest", "getContent success. result = " + result.length());
                try {
                    new JSONObject(result);
                    CacheUtils.writeFile(ConfigUtil.instance().mFileName + DeviceUtil.instance().appVersionCode(AppMain.getApp()), result);
                    GTConfig.instance().saveConfigVersion(versionCode);
                    Logger.i("ConfigFileRequest", "getContent. save config file success.");
                } catch (JSONException e) {
                    Logger.e("ConfigFileRequest", e);
                }
            }

            @Override
            public void onFailure(Request request, IOException e) {
                Logger.e("ConfigFileRequest", e);
            }
        });
    }

}
